<?php

namespace App\Models;

class Articles extends Base
{
    protected $table = 'articles';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">待审核</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--info">未通过</span>',
    ];

    protected $fillable = [
        'id','title','content','status','view','created_at','updated_at'
    ];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($fields = ['*'], $limit = 10){
        $lists = $this->select($fields)->where('status','<>',-1)->orderBy('created_at', 'desc')->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = '', $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        $params['status'] = 1;
        if(isset($page)) {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->where('id',$params['id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['hot']) && $params['hot']) {
                        $query ->where('hot',$params['hot']);
                    }
                    if(isset($params['good']) && $params['good']) {
                        $query ->where('good',$params['good']);
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $query ->where("title", "like", "%$keyword%");
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['hot']) && $params['hot']) {
                        $query ->where('hot',$params['hot']);
                    }
                    if(isset($params['good']) && $params['good']) {
                        $query ->where('good',$params['good']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_id',$params['cid'])->orwhere('cate_pid',$params['cid']);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort,'desc')->limit($limit)->get();
        }
        return $lists;
    }

    /**
     * 根据id查询一条新闻
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function details($id, $fields = ['*']){
        return $this->select($fields)->where($id)->where('status','<>',-1)->first();
    }

}
